package com.calligraphy.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;

/**
 * @Author hy
 * @Date 2019/1/8 18:31
 **/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {

    //@HystrixCommand(fallbackMethod = "fallback")(单个配置)
    //HystrixCommandProperties
    //超时配置
//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//    })

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),       //设置熔断
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
    })
    /**
     *  单独设置时间
     * @return
     */
    //@HystrixCommand(commandKey="www")
    @RequestMapping("/msg")
    public String getAll(){
        RestTemplate restTemplate = new RestTemplate();
        String msg = restTemplate.postForObject("http://localhost:8083/product/msg","你是条狗吧",String.class);
        log.info(msg);
        return msg ;
    }

    public String fallback(){

        return "太拥挤,我在你的世界里";
    }

    public String defaultFallback(){
        return "太拥挤,我在你的世界里";
    }
}
